import { _decorator, Component, Label, Node } from 'cc';
const { ccclass, property } = _decorator;

import { AIDifficulty } from "../Gomoku_AI";
import Gomoku_GameManager from "../Gomoku_GameManager";

@ccclass('GomokuDifficultyDisplay')
export default class Gomoku_DifficultyDisplay extends Component {

    @property(Label)
    difficultyLabel: Label | null = null;

    @property(Node)
    Button_switch: Node | null = null;
    onLoad() {
        this.addListeners();
        this.updateDisplay();
    }
    addListeners() {
        if (this.Button_switch) {
            this.Button_switch.on(Node.EventType.TOUCH_END, this.On_Button_switch, this);
        }
    }
    On_Button_switch() {
        if (Gomoku_GameManager.Instance) {
            const currentDifficulty = Gomoku_GameManager.Instance.currentDifficulty;
            let nextDifficulty: AIDifficulty;

            // 循环切换难度：简单 -> 中等 -> 困难 -> 简单
            switch (currentDifficulty) {
                case AIDifficulty.Easy:
                    nextDifficulty = AIDifficulty.Medium;
                    break;
                case AIDifficulty.Medium:
                    nextDifficulty = AIDifficulty.Hard;
                    break;
                case AIDifficulty.Hard:
                    nextDifficulty = AIDifficulty.Easy;
                    break;
                default:
                    nextDifficulty = AIDifficulty.Medium;
            }

            Gomoku_GameManager.Instance.setDifficulty(nextDifficulty);
            this.updateDisplay();
            this.playButtonAudio();
        }
    }
    updateDisplay() {
        if (this.difficultyLabel && Gomoku_GameManager.Instance) {
            const difficultyName = Gomoku_GameManager.Instance.getCurrentDifficultyName();
            this.difficultyLabel.string = `AI难度: ${difficultyName}`;
        }
    }
    playButtonAudio() {
        // // 播放按钮音效
        // const AudioManager = require("../../../Script/Manager/AudioManager").default;
        // if (AudioManager) {
        //     AudioManager.PlayButtonAudio();
        // }
    }
    //    // 每帧更新显示（确保显示最新状态）
    update() {
        this.updateDisplay();
    }
}